/*
 * InvoiceItem.java
 * 
 * @version 1.0
 * CS 2340 Team 10: Cyberdyne Systems
 */

package edu.gatech.oad.cyberdoc.domain;

import java.text.NumberFormat;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Represents a single line item on an invoice.
 * 
 * @author John Girata
 * @version 1.0
 */
public class InvoiceItem implements JSONSerializable {
	/**
	 * The logger for this class.
	 */
	private static final Logger LOG;

	/**
	 * The name of the service provided.
	 */
	private String name;

	/**
	 * The description of the service provided.
	 */
	private String description;

	/**
	 * The cost of the service provided.
	 */
	private double cost;

	static {
		LOG = Logger.getLogger("edu.gatech.oad.cyberdoc.domain.InvoiceItem");
	}

	/**
	 * Creates a new InvoiceItem with required information.
	 * 
	 * @param name
	 *            The name of the service provided
	 * @param description
	 *            The description of the service provided
	 * @param cost
	 *            The cost of the service provided
	 */
	public InvoiceItem(String name, String description, double cost) {
		this.name = name;
		this.description = description;
		this.cost = cost;

		LOG.info("New InvoiceItem \"" + name + "\" created.");
	}

	/**
	 * Creates an instance of this class from a JSONObject, as required by
	 * DatabaseSerializable.
	 * 
	 * @param object
	 *            The information from the database to use to create this User
	 * @throws JSONException
	 *             If this constructor is given an improperly formatted
	 *             exception
	 */
	public InvoiceItem(JSONObject object) throws JSONException {
		name = object.getString("name");
		description = object.getString("description");
		cost = object.getDouble("cost");
	}

	/**
	 * Returns a string representation of this InvoiceItem.
	 * 
	 * The InvoiceItem is displayed in the following format:
	 * 
	 * "name": $"cost"
	 * 
	 * @return This InvoiceItem as a String
	 */
	@Override
	public String toString() {
		final String costString = NumberFormat.getInstance().format(cost);

		return name + ": $" + costString;
	}

	/**
	 * @return the name of the service provided
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name of the service provided
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the cost
	 */
	public double getCost() {
		return cost;
	}

	/**
	 * @param cost
	 *            the cost to set
	 */
	public void setCost(double cost) {
		this.cost = cost;
	}

	/**
	 * Serializes this object into a JSONObject.
	 * 
	 * @return The JSON representation of this class
	 */
	@Override
	public JSONObject toJSONObject() {
		final JSONObject object = new JSONObject();

		try {
			object.put("name", name);
			object.put("description", description);
			object.put("cost", cost);
		} catch (JSONException e) {
			LOG.log(Level.SEVERE, "Unexpected exception", e);
		}

		return object;
	}
}
